using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gko.Utils
{
	/// <summary>
	/// Extensions for IList interaface
	/// </summary>
	public static class ListExtensions
	{
		/// <summary>
		/// Adds the range of items to list.
		/// </summary>
		/// <param name="list">The list.</param>
		/// <param name="items">The items.</param>
		/// <exception cref="ArgumentNullException"><c>list</c> or <c>items</c> is null.</exception>
		/// <exception cref="System.NotSupportedException">The System.Collections.IList is read-only.  -or- The System.Collections.IList has a fixed size.</exception>
		public static void AddRange(this IList list, IEnumerable items)
		{
			if (list == null) {
				throw new ArgumentNullException("list");
			}
			if (items == null) {
				throw new ArgumentNullException("items");
			}
			items.ForEach(i => list.Add(i));
		}

		/// <summary>
		/// Swaps two items.
		/// </summary>
		/// <typeparam name="T">Type of list item</typeparam>
		/// <param name="list">Source list</param>
		/// <param name="firstIndex">First index</param>
		/// <param name="secondIndex">Second index</param>
		public static void Swap<T>(this IList<T> list, int firstIndex, int secondIndex)
		{
			var first = list[firstIndex];
			var second = list[secondIndex];
			ObjectUtility.Swap(ref first, ref second);
			list[firstIndex] = first;
			list[secondIndex] = second;
		}

		/// <summary>
		/// Swaps two items.
		/// </summary>
		/// <param name="list">Source list</param>
		/// <param name="firstIndex">First index</param>
		/// <param name="secondIndex">Second index</param>
		public static void Swap(this IList list, int firstIndex, int secondIndex)
		{
			var first = list[firstIndex];
			var second = list[secondIndex];
			ObjectUtility.Swap(ref first, ref second);
			list[firstIndex] = first;
			list[secondIndex] = second;
		}
	}
}